<?php
include_once(TOOL.'Html.php');
include_once(TOOL.'Dir.php');

class Menu
{
    const srcEnd = '.md';
    const targetEnd = '.html';
    
    public static $nodeClassChain = [];
    
    //生成hash值, 避免中文乱码, 以及减少长度
    public static function getHash($str, $len=10)
    {
        return substr(md5($str), 0, $len);
    }
    
    //去掉排序用的字符串
    public static function trimOrder($str)
    {
        return preg_replace('#_\d+_#i', '', $str);
    }
    
    public static function getMenu($dir, $title)
    {
        rtrim($dir, DIRECTORY_SEPARATOR);
        $dir .= DIRECTORY_SEPARATOR;
    
        $files = Dir::ini($dir)
            ->extension('md')
            ->replace($dir, '')
            ->replace(self::srcEnd, '')
            ->sortFiles()
            ->pregReplace('_\d+_', '')
            ->fileList;
    
        //file_put_contents('./a.log', var_export($files, TRUE));
        
        $already = array();
        $menu = array();
        $step = 20; //px
        $startPadding = 20; //px
        
        foreach ($files as $file) {
            $nodeClassChain = [];
            
            if (strpos($file, DIRECTORY_SEPARATOR) === FALSE) { //根目录文件
                $fileName = ($file == 'index') ? '开始' : '';
                $menu[] = p::ini()
                    ->setText($fileName)
                    ->setAttr('id', 'root')
                    ->setAttr('data-parent', 'root')
                    ->setAttr('data-uri', $file.self::targetEnd)
                    ->setAttr('style', 'padding-left:'.$startPadding.'px')
                    //->setAttr('onclick', 'redirect(this)')
                    ->addClass('file');
            } else {
                
                //目录
                $arr = explode(DIRECTORY_SEPARATOR, $file);
                $last = count($arr) - 1;
                $path = '';
                foreach ($arr as $k => $item) {
                    $parentPath = $path;
                    $path = $path.$item;
                    
                    $hashPath = self::getHash($path);
                    $hashParent = self::getHash($parentPath);
                    $nodeClassChain[$hashPath] = '1';
                    
                    $padding = $k*$step + $startPadding;
                    $padding = 'padding-left:'.$padding.'px';
                    if (empty($already[$hashPath])) {
                        $already[$hashPath] = $path;
                        $obj = p::ini()
                            ->setAttr('style', $padding)
                            ->addClass($hashParent)
                            ->addClass('l-'.($k+1));
                        
                        if ($k != $last) {
                            $obj->setText('+ '.$item)
                                ->setAttr('id', $hashPath)
                                //->setAttr('onclick', 'toggle(this)')
                                ->addClass('dir');
                                
                        } else {
                            $uri = str_replace(DIRECTORY_SEPARATOR, '/', $file).'.html?path='.$hashParent.'&id='.$hashPath;
                            $obj->setText($item)
                                ->setAttr('id', $hashPath)
                                ->setAttr('data-uri', $uri)
                                //->setAttr('onclick', 'redirect(this)')
                                ->addClass('file');
                                
                        }
    
                        $menu[] = $obj;
                    }
                }
            }
            
            self::$nodeClassChain[] = $nodeClassChain;
        }
        
        $docTitle = p::ini()->setText($title)->addClass('root-title');
        $root = tag::ini('div')
            ->addClass('root')
            ->append($docTitle);
        
        foreach ($menu as $item) {
            $root->append($item);
        }
        
        return $root->out();
        
    }
    
}